package com.clay.appointment.controller.api;

import com.clay.appointment.common.page.PageInfo;
import com.clay.appointment.common.response.Result;
import com.clay.appointment.entity.hospital.Department;
import com.clay.appointment.entity.hospital.Hospital;
import com.clay.appointment.entity.hospital.Schedule;
import com.clay.appointment.service.DepartmentService;
import com.clay.appointment.service.HospitalService;
import com.clay.appointment.service.HospitalSetService;
import com.clay.appointment.service.ScheduleService;
import com.clay.appointment.vo.hospital.DepartmentQueryVo;
import com.clay.appointment.vo.hospital.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author : clay
 * @Description:
 * @Date: Create in 21:52 2021/7/30
 * @Modified By:
 */
@Api(tags = "医院调用API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;


    @ApiOperation("查询排班")
    @PostMapping("/schedule/list")
//    public Result showSchedule(HttpServletRequest request) {
    public Result showSchedule(@ApiParam(value = "医院编号") @Param("hospitalCode") String hospitalCode,
                               @ApiParam(value = "当前页") @Param("page") Integer page,
                               @ApiParam(value = "每页记录数") @Param("limit") Integer limit,
                               @ApiParam(value = "密钥") @Param("sign") String sign) {

        if(hospitalSetService.JudgeSign(sign,hospitalCode))
        {
            ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
            scheduleQueryVo.setHospitalCode(hospitalCode);
            PageInfo<Schedule> schedulePageInfo = scheduleService.selectPage(page, limit, scheduleQueryVo);
            return Result.success(schedulePageInfo);
        }

        //签名校验
//        Map<String, Object> paramMap = hospitalSetService.JudgeSign(request);
//        String hospitalCode = (String) paramMap.get("hospitalCode");
//        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
//        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));
       return Result.error();
    }


    @ApiOperation("上传排班")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //签名校验
        Map<String, Object> paramMap = hospitalSetService.JudgSign(request);
        scheduleService.save(paramMap);
        return Result.success();
    }


    @ApiOperation("删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        //签名校验
        Map<String, Object> paramMap = hospitalSetService.JudgSign(request);
        String hospitalCode = (String) paramMap.get("hospitalCode");
        String hospitalScheduleId = (String) paramMap.get("hospitalScheduleId");
        scheduleService.removeSchedule(hospitalCode, hospitalScheduleId);

        return Result.success();
    }


    @ApiOperation("删除科室")
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = hospitalSetService.JudgSign(request);
        String hospitalCode = (String) paramMap.get("hospitalCode");
        String departmentCode = (String) paramMap.get("departmentCode");
        departmentService.removeDepartment(hospitalCode, departmentCode);
        return Result.success();
    }

    @ApiOperation("查询科室接口")
    @PostMapping("/department/list")
    public Result showDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = hospitalSetService.JudgSign(request);
        String hospitalCode = (String) paramMap.get("hospitalCode");
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHospitalCode(hospitalCode);
        int page = Integer.parseInt((String) paramMap.get("page"));
        int limit = Integer.parseInt((String) paramMap.get("limit"));
        PageInfo<Department> pageInfo = departmentService.selectPage(page, limit, departmentQueryVo);
        return Result.success(pageInfo);
    }


    @ApiOperation("上传课室接口")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //
        Map<String, Object> paramMap = hospitalSetService.JudgSign(request);
        departmentService.save(paramMap);
        return Result.success();
    }


    @ApiOperation("上传医院接口")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, Object> paramMap = hospitalSetService.JudgSign(request);
        //调用service
        hospitalService.save(paramMap);
        return Result.success();
    }

    @ApiOperation("查询医院数据接口")
    @PostMapping("/hospital/show")
    public Result showHospital(HttpServletRequest request) {
        Map<String, Object> paramMap = hospitalSetService.JudgSign(request);
        //调用service
        Hospital hospital = hospitalService.getHospitalByHospitalCode((String) paramMap.get("hospitalCode"));
        return Result.success(hospital);
    }


}
